package game.module.ddz.logic;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.module.ddz.bean.DdzRoom;

public class DdzDataManager {

	private static Logger logger = LoggerFactory.getLogger(DdzDataManager.class);

	static class SingletonHolder {
		static DdzDataManager instance = new DdzDataManager();
	}

	public static DdzDataManager getInstance() {
		return SingletonHolder.instance;
	}

	private Map<String, DdzRoom> roomMap = new ConcurrentHashMap<>();

	private Map<Integer, String> playerRoomMap = new ConcurrentHashMap<>();

	public boolean isPlayerInRoom(int playerId) {
		return playerRoomMap.containsKey(playerId);
	}

	public void removeRoom(String roomUuid) {
		DdzRoom ddzRoom = roomMap.get(roomUuid);
		if (ddzRoom == null) {
			return;
		}
		List<Integer> playerList = ddzRoom.getPlayerList();
		for (Integer aPlayerId : playerList) {
			playerRoomMap.remove(aPlayerId);
		}
		playerList.clear();
		roomMap.remove(roomUuid);
	}

	public boolean hasRoomUuid(String roomUuid) {
		return roomMap.containsKey(roomUuid);
	}

	public void addRoom(DdzRoom ddzRoom) {
		roomMap.put(ddzRoom.getUuid(), ddzRoom);
		playerRoomMap.put(ddzRoom.getPlayerList().get(0), ddzRoom.getUuid());
	}

	public void addPlayer(int playerId,String roomId) {
		playerRoomMap.put(playerId, roomId);
	}
	
	public boolean checkRoomExist(String room_id) {
		return roomMap.containsKey(room_id);
	}

	public DdzRoom getDdzRoom(String room_id) {
		return roomMap.get(room_id);
	}

	public DdzRoom getDdzRoomByPlayerId(int playerId) {
		String roomId = playerRoomMap.get(playerId);
		if (roomId == null) {
			return null;
		}
		return roomMap.get(roomId);
	}
	
	public int getOnlineSize(){
		int ret = 0;
		for(DdzRoom aRoom : roomMap.values()){
			ret += aRoom.getPlayerList().size();
		}
		return ret;
	}

}
